﻿Option Infer On
Option Explicit On
Option Strict On

Imports System
Imports GenDotNet.task.common

Public Enum StandardNetType
   Unknown = 0
   [String]
   [Byte]
   Int16
   Int32
   Int64
   UInt32
   Uint64
   DateTime
   SmartDate
   [Boolean]
   [Object]
   Guid
   ByteArray
   [Single]
   [Double]
   [Decimal]
End Enum


Public Module NetTypeExtensions
   Public Function XsdToNetType(ByVal type As String) As StandardNetType
      If type.StartsWith("System.") Then
         type = type.Replace("System.", String.Empty)
      End If
      If type.EqualsIgnoreCase("string") Then
         Return StandardNetType.String
      ElseIf type.EqualsIgnoreCase("integer") OrElse type.EqualsIgnoreCase("int32") Then
         Return StandardNetType.Int32
      ElseIf type.EqualsIgnoreCase("boolean") Then
         Return StandardNetType.Boolean
      ElseIf type.EqualsIgnoreCase("date") OrElse type.EqualsIgnoreCase("datetime") Then
         Return StandardNetType.DateTime
      ElseIf type.EqualsIgnoreCase("Guid") Then
         Return StandardNetType.Guid
      ElseIf type.EqualsIgnoreCase("Byte") Then
         Return StandardNetType.Byte
      ElseIf type.EqualsIgnoreCase("Single") Then
         Return StandardNetType.Single
      ElseIf type.EqualsIgnoreCase("Double") Then
         Return StandardNetType.Double
      ElseIf type.EqualsIgnoreCase("Decimal") Then
         Return StandardNetType.Decimal
      Else
         Throw New ArgumentException("Type not found")
      End If
   End Function

   <System.Runtime.CompilerServices.Extension()> _
      Public Function Output(ByVal type As StandardNetType) As String
      Select Case type
         Case StandardNetType.String
            Return "System.String"
         Case StandardNetType.Int16
            Return "System.Int16"
         Case StandardNetType.Int32
            Return "System.Int32"
         Case StandardNetType.Int64
            Return "System.Int64"
         Case StandardNetType.DateTime
            Return "System.DateTime"
         Case StandardNetType.Uint64
            Return "System.UInt64"
         Case StandardNetType.UInt32
            Return "System.UInt32"
         Case StandardNetType.Boolean
            Return "System.Boolean"
         Case StandardNetType.Object
            Return "System.Object"
         Case StandardNetType.Byte
            Return "System.Byte()"
         Case StandardNetType.Guid
            Return "System.Guid"
         Case StandardNetType.Decimal
            Return "System.Decimal"
         Case Else
            Return "Unknown Type"
      End Select
   End Function


   <System.Runtime.CompilerServices.Extension()> _
  Public Function ShortName(ByVal type As StandardNetType) As String
      If type.Output.StartsWith("System.") Then
         Return type.Output.Replace("System.", "")
      End If
      Return type.Output
   End Function

End Module
